package com.itstudy.service.impl;

import com.itstudy.mapper.UserMapper;
import com.itstudy.pojo.User;
import com.itstudy.service.UserService;
import com.itstudy.utils.Md5Util;
import com.itstudy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserviceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User selectByName(String userName) {
        return userMapper.selectByName(userName);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void insertIntoUser(String userName, String passWord) {
        //对密码先进行MD5加密
        String md5String = Md5Util.getMD5String(passWord);
        userMapper.insertIntoUser(userName,md5String);
        return;
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> o = ThreadLocalUtil.get();
        Integer id = (Integer) o.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String password,Integer id) {
        userMapper.updatePwd(password,id);
    }
}
